﻿#region License Info

//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#endregion


using System.Windows.Forms;
using ABMath.ModelFramework.Data;

namespace ExtraControlLibrary.DirectedGraphs
{
    public partial class ScrollableDAGViewer : UserControl
    {
        private const double VirtualMinX = -2;
        private const double VirtualMaxX = 3;
        private const double VirtualMinY = -2;
        private const double VirtualMaxY = 3;

        public DirectedGraphViewer DAGViewer { get { return dagViewer; } }

        public DirectedGraph Graph { get { return DAGViewer.Graph; } set { DAGViewer.Graph = value; } }

        protected void OnViewPortChange(double minX, double minY, double maxX, double maxY)
        {
            // fix scroll bars
            double xSpan = VirtualMaxX - VirtualMinX;
            double ySpan = VirtualMaxY - VirtualMinY;

            hScrollBar1.LargeChange = (int)((maxX - minX)/xSpan*1000);
            hScrollBar1.Value = (int)((minX - VirtualMinX)/xSpan*1000);
            vScrollBar1.LargeChange = (int)((maxY - minY)/ySpan*1000);
            vScrollBar1.Value = (int)((VirtualMaxY - maxY)/ySpan*1000);
        }

        void UpdateView()
        {
            double x1 = (hScrollBar1.Value/1000.0)*(VirtualMaxX - VirtualMinX) + VirtualMinX;
            double x2 = ((hScrollBar1.Value + hScrollBar1.LargeChange) / 1000.0) * (VirtualMaxX - VirtualMinX) + VirtualMinX;
            double y2 = VirtualMaxY - (vScrollBar1.Value / 1000.0) * (VirtualMaxY - VirtualMinY);
            double y1 = VirtualMaxY - ((vScrollBar1.Value + vScrollBar1.LargeChange) / 1000.0) * (VirtualMaxY - VirtualMinY);

            dagViewer.ViewMinX = x1;
            dagViewer.ViewMaxX = x2;
            dagViewer.ViewMinY = y1;
            dagViewer.ViewMaxY = y2;
            dagViewer.RebuildAll(false);
            dagViewer.Refresh();
        }

        public ScrollableDAGViewer()
        {
            InitializeComponent();

            dagViewer.OnViewPortChange = this.OnViewPortChange;
            OnViewPortChange(dagViewer.ViewMinX, dagViewer.ViewMinY, dagViewer.ViewMaxX, dagViewer.ViewMaxY);
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            UpdateView();
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            UpdateView();
        }

    }
}
